View Javadoc
1 package jrre; 2 3 import jrre.event.*; 4 5 import jrre.types.*; 6 import jrre.gui.*; 7 import jrre.instructionset.Instruction; 8 9 import java.util.*; 10 import java.beans.*; 11 12 /*** 13 * 14 * 15 * @author Christopher Ellsworth (Chris@chrisellsworth.com) 16 * @author Clarence Alston (massclax@hotmail.com) 17 */ 18 public class Stack { 19 20 private static JavaStackFrameGui stackFrameGui; 21 22 private static StackFrame stackPointer; 23 private static StackFrame nextFrame; 24 private static StackFrame previousFrame; 25 26 static { 27 if(JRRE.guiOn()){ 28 stackFrameGui = new JavaStackFrameGui(); 29 } 30 } 31 32 /*** 33 * Adds a listener to the collection of classes to notify when a 34 * property changes. 35 */ 36 /* 37 public void addPropertyChangeListener(PropertyChangeListener listener) { 38 propertyChange.addPropertyChangeListener(listener); 39 } 40 */ 41 42 public static StackFrame getNextFrame(){ 43 return nextFrame; 44 } 45 46 public static void setNextFrame(StackFrame newFrame){ 47 nextFrame = newFrame; 48 } 49 50 public static jrre.api.java.lang.Class getClassContainingMethod(){ 51 return stackPointer.getClassContainingMethod(); 52 } 53 54 /*** 55 * Gets the StackPointer. 56 */ 57 public static StackFrame getStackPointer(){ 58 return stackPointer; 59 } 60 61 /*** 62 * Sets the StackPointer. 63 * @param StackPointer The value to set it to. 64 */ 65 public static void setStackPointer(StackFrame newStackPointer){ 66 stackPointer = newStackPointer; 67 } 68 69 public static void clearStack(){ 70 71 nextFrame = null; 72 previousFrame = null; 73 74 if(JRRE.guiOn()) 75 stackFrameGui.clear(); 76 77 if(stackPointer != null) 78 stackPointer.clear(); 79 //operandStack.clear(); 80 } 81 82 public static void pushInitial(StackFrame toPush, String methodSignature){ 83 stackPointer = toPush; 84 85 if(JRRE.guiOn()){ 86 JavaStackFrameGui.push(methodSignature); 87 OperandFrameGui.pushFrame("Method: "+methodSignature); 88 } 89 } 90 91 public static void push(StackFrame toPush, String methodSignature){ 92 93 94 if(JRRE.getVerbose()) 95 System.out.println("Pushing Stack Frame: "+methodSignature); 96 97 setNextFrame(toPush); 98 getNextFrame().setPreviousFrame(stackPointer); 99 stackPointer = getNextFrame(); 100 101 if(JRRE.guiOn()){ 102 JavaStackFrameGui.push(methodSignature); 103 OperandFrameGui.pushFrame("Method: "+methodSignature); 104 } 105 106 PushStackFrameEvent event = new PushStackFrameEvent(methodSignature, 107 toPush.getMaxStackSize(), 108 toPush.getLocalVariableSize(), 109 toPush.getInstructions()); 110 JRRE.processEvent(event); 111 112 113 } 114 115 public static void pop(){ 116 117 if(JRRE.getVerbose()) 118 System.out.println("Popping Stack Frame: "); 119 stackPointer = stackPointer.getPreviousFrame(); 120 121 if(stackPointer != null) 122 setNextFrame(null); 123 124 125 if(JRRE.guiOn()){ 126 JavaStackFrameGui.pop(); 127 OperandFrameGui.popFrame(); 128 } 129 130 PopStackFrameEvent event = new PopStackFrameEvent(":-)"); 131 JRRE.processEvent(event); 132 133 } 134 135 public static void popReturn(Type returnValue){ 136 pop(); 137 pushOperand(returnValue); 138 } 139 140 141 public static Type getLocalVariable(int variableIndex){ 142 return stackPointer.getLocalVariable(variableIndex); 143 } 144 145 public static void setLocalVariable(int variableIndex, Type value){ 146 147 stackPointer.setLocalVariable(variableIndex, value); 148 } 149 150 public static void pushOperand(Type variable){ 151 152 String value = new String(); 153 String name = new String(); 154 155 if(variable instanceof PrimitiveType){ 156 value = ""+((PrimitiveType)variable).getValue(); 157 name = "PrimitiveType"; 158 } 159 else if(variable instanceof ReferenceType){ 160 value = ((ReferenceType)variable).toString(); 161 name = "ReferenceType"; 162 163 } 164 165 PushOperandEvent event = new PushOperandEvent(name, value); 166 JRRE.processEvent(event); 167 168 stackPointer.getOperandStack().push(variable); 169 } 170 171 public static Type popOperand(){ 172 173 PopOperandEvent event = new PopOperandEvent(); 174 JRRE.processEvent(event); 175 176 return stackPointer.getOperandStack().pop(); 177 } 178 179 public static Instruction getNextInstruction(){ 180 181 if(stackPointer == null) 182 return null; 183 else 184 return stackPointer.getNextInstruction(); 185 } 186 187 public static Instruction getPrevInstruction(){ 188 189 if(stackPointer == null) 190 return null; 191 else 192 return stackPointer.getPrevInstruction(); 193 } 194 195 public static Instruction getCurrentInstruction(){ 196 197 return stackPointer.getCurrentInstruction(); 198 } 199 200 public static void restoreLastInstruction(){ 201 202 stackPointer.restoreLastInstruction(); 203 } 204 205 public static void executeClassInitFrame(StackFrame initFrame){ 206 207 if(JRRE.getVerbose()) 208 System.out.println("\nExecuting Class Init "+initFrame+"\n"); 209 210 push(initFrame, "<clinit>()V::"+initFrame.getClassContainingMethod().getFullyQualifiedName()); 211 212 /* 213 Instruction instruction = Stack.getNextInstruction(); 214 while(instruction != null){ 215 instruction.execute(); 216 instruction = Stack.getNextInstruction(); 217 } 218 219 System.out.println("\nEnd Class Init\n"); 220 */ 221 222 //pop(); 223 } 224 225 /*** 226 * @task Offset's dont seem right, althought test cases are working. 227 */ 228 public static void branch(int offset){ 229 230 if(JRRE.getVerbose()) 231 System.out.println("Branching: "+offset); 232 233 if(offset > 0){ 234 for(int i=1;i < offset;i++){ 235 236 Instruction cursor = getNextInstruction(); 237 if(JRRE.getVerbose()) 238 System.out.println("offset: "+offset+" index: "+i+" skipping: "+cursor); 239 i += cursor.getLength(); 240 } 241 } 242 else{ 243 offset=offset*-1; 244 245 restoreLastInstruction(); 246 for(int i=2;i < offset;i++){ 247 248 restoreLastInstruction(); 249 if(JRRE.getVerbose()) 250 System.out.println("offset: "+offset+" index: "+i+" skipping: "+getCurrentInstruction()); 251 i += getCurrentInstruction().getLength(); 252 } 253 } 254 255 } 256 }

This page was automatically generated by Maven